package com.easyspringboot.bulider;

import com.easyspringboot.bean.Constants;
import com.easyspringboot.bean.FieldInfo;
import com.easyspringboot.bean.TableInfo;
import com.easyspringboot.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * @Author xiaoyi
 * @Version 1.0
 * @ClassName BuildMapper
 * @Time 3:38
 */
public class BuildController {
    private static final Logger logger = LoggerFactory.getLogger(BuildController.class);

    public static void execute(TableInfo tableInfo) {
        File folder = new File(Constants.PATH_CONTROLLER);
        String paramsNames= StringUtils.lowerCaseFirstLetter(tableInfo.getBeanName())+Constants.SUFFIX_BEAN_SERVICE;
        String beanMapperName = tableInfo.getBeanName()+Constants.SUFFIX_BEAN_MAPPERS;
        if (!folder.exists()) {
            folder.mkdirs();
        }
        File poFile = new File(folder, tableInfo.getBeanName() + Constants.SUFFIX_BEAN_CONTROLLER + ".java");
        OutputStream out = null;
        OutputStreamWriter outw = null;
        BufferedWriter bw = null;
        try {
            out = new FileOutputStream(poFile);
            outw = new OutputStreamWriter(out, "utf8");
            bw = new BufferedWriter(outw);
            bw.write("package " + Constants.PACKAGE_CONTROLLER + ";");
            bw.newLine();
            bw.newLine();
            bw.write("import " + Constants.PACKAGE_VO + ".ResponseVO;");
            bw.newLine();
            bw.write("import " + Constants.PACKAGE_VO + ".PaginationResultVO;");
            bw.newLine();
            bw.write("import " + Constants.PACKAGE_PO +"."+tableInfo.getBeanName() + ";");
            bw.newLine();
            bw.write("import " + Constants.PACKAGE_QUERY +"."+tableInfo.getBeanName()+Constants.SUFFIX_BEAN_QUERY + ";");
            bw.newLine();
            bw.write("import org.springframework.web.bind.annotation.RequestBody;");
            bw.newLine();
            bw.write("import org.springframework.web.bind.annotation.RequestMapping;");
            bw.newLine();
            bw.write("import org.springframework.web.bind.annotation.RestController;");
            bw.newLine();
            bw.write("import javax.annotation.Resource;");
            bw.newLine();
            bw.write("import " + Constants.PACKAGE_SERVICE + "." + tableInfo.getBeanName() + Constants.SUFFIX_BEAN_SERVICE + ";");
            bw.newLine();
            bw.write("import java.util.List;");
            bw.newLine();

            //创建类注释
            BuildComment.createClassComment(bw, tableInfo.getComment() + "Controller");
            bw.newLine();

            bw.write("@RestController");
            bw.newLine();
            bw.write("@RequestMapping(\"/" +StringUtils.lowerCaseFirstLetter(tableInfo.getBeanName())  + "\")");
            bw.newLine();
            bw.write("public class " + tableInfo.getBeanName() + Constants.SUFFIX_BEAN_CONTROLLER +"  extends ABaseController{");
            bw.newLine();
            bw.write("\t@Resource");
            bw.newLine();
            bw.write("\tprivate " + tableInfo.getBeanName() + Constants.SUFFIX_BEAN_SERVICE + " " +paramsNames+ ";");
            bw.newLine();
            //新增
            bw.write("\t//新增");
            bw.newLine();
            bw.write("\t@RequestMapping(\"add\")");
            bw.newLine();
            bw.write("\tpublic ResponseVO add( " + tableInfo.getBeanName() + " " + StringUtils.lowerCaseFirstLetter(tableInfo.getBeanName()) + "){");
            bw.newLine();
            bw.write("\t\t"+paramsNames+".add(" + StringUtils.lowerCaseFirstLetter(tableInfo.getBeanName()) + ");");
            bw.newLine();
            bw.write("\t\treturn getSuccessResponseVO(null);");
            bw.newLine();
            bw.write("\t}");
            bw.newLine();

            //根据条件分页查询
            bw.write("\t//根据条件分页查询");
            bw.newLine();
            bw.write("\t@RequestMapping(\"loadDateList\")");
            bw.newLine();
            bw.write("\tpublic ResponseVO"+" loadDateList( "+tableInfo.getBeanName()+Constants.SUFFIX_BEAN_QUERY+" " +" query){");
            bw.newLine();
            bw.write("\t\treturn getSuccessResponseVO(" + paramsNames + ".findListByPage(query));");
            bw.newLine();
            bw.write("\t}");
            bw.newLine();
            //批量新增
            bw.write("\t//批量新增");
            bw.newLine();
            bw.write("\t@RequestMapping(\"addBatch\")");
            bw.newLine();
            bw.write("\tpublic ResponseVO addBatch(@RequestBody List<" + tableInfo.getBeanName() + "> " + "listBatch){");
            bw.newLine();
            bw.write("\t\t"+paramsNames+".addBatch(listBatch);");
            bw.newLine();
            bw.write("\t\treturn getSuccessResponseVO(null);");
            bw.newLine();
            bw.write("\t}");
            bw.newLine();

            //批量新增/更新
            bw.write("\t//批量新增/更新");
            bw.newLine();
            bw.write("\t@RequestMapping(\"addOrUpdateBatch\")");
            bw.newLine();
            bw.write("\tpublic ResponseVO addOrUpdateBatch(@RequestBody List<" + tableInfo.getBeanName() + "> " + "listBatch){");
            bw.newLine();
            bw.write("\t\t"+paramsNames+".addOrUpdateBatch(listBatch);");
            bw.newLine();
            bw.write("\t\treturn getSuccessResponseVO(null);");
            bw.newLine();
            bw.write("\t}");
            bw.newLine();

            for (Map.Entry<String, List<FieldInfo>> entry : tableInfo.getKeyIndexMap().entrySet()) {
                String methodNameStr = null;
                List<FieldInfo> keyFieldInfos = entry.getValue();//主键字段信息
                Integer index = 0;
                Integer index_annotation = 0;
                StringBuffer methodName = new StringBuffer();//方法名
                StringBuffer annotationName = new StringBuffer();//注解名
                StringBuffer methodParams = new StringBuffer();//方法名
                StringBuffer paramsName = new StringBuffer();//参数名
                for (FieldInfo fieldInfo : keyFieldInfos) {
                    methodName.append(StringUtils.uperCaseFirstLetter(fieldInfo.getPropertyName()));

                    annotationName.append(fieldInfo.getComment());
                    if (index < methodParams.length() - 1) {
                        methodParams.append(",");
                        paramsName.append(",");
                    }
                    if (index_annotation < keyFieldInfos.size() - 1) {
                        annotationName.append(",");
                    }
                    if (index < keyFieldInfos.size() - 1) {
                        methodName.append("And");
                        methodNameStr = methodName.substring(0, methodName.length() - 3);
                    } else {
                        methodNameStr = methodName.toString();
                    }
                    methodParams.append(fieldInfo.getJavaType() + " " + fieldInfo.getPropertyName());
                    paramsName.append(fieldInfo.getPropertyName());
                }
                //查询
                bw.newLine();
                bw.write("\t//通过" + methodNameStr + "查询");
                bw.newLine();
                bw.write("\t@RequestMapping(\"get" + methodNameStr + "\")");
                bw.newLine();
                bw.write("\tpublic ResponseVO"+" get" + methodNameStr + "(  " + methodParams + ") {");
                bw.newLine();
                bw.write("\t\treturn getSuccessResponseVO(" + paramsNames + ".getBy"+methodNameStr+"("+paramsName+"));");
                bw.newLine();
                bw.write("\t}");
                bw.newLine();
                //修改
                bw.newLine();
                bw.write("\t//通过" + methodNameStr + "修改");
                bw.newLine();
                bw.write("\t@RequestMapping(\"update" + methodNameStr + "\")");
                bw.newLine();
                bw.write("\tpublic ResponseVO"+" update" + methodNameStr + "( " +tableInfo.getBeanName() + " bean , "  + methodParams + ") {");
                bw.newLine();
                bw.write("\t\t"+paramsNames+".updateBy"+methodNameStr+"(bean,"+paramsName+");");
                bw.newLine();
                bw.write("\t\treturn getSuccessResponseVO(null);");
                bw.newLine();
                bw.write("\t}");
                bw.newLine();
                //删除
                bw.newLine();
                bw.write("\t//通过" + methodNameStr + "删除");
                bw.newLine();
                bw.write("\t@RequestMapping(\"delete" + methodNameStr + "\")");
                bw.newLine();
                bw.write("\tpublic ResponseVO"+" delete" + methodNameStr + "("+ methodParams + ") {");
                bw.newLine();
                bw.write("\t\t"+paramsNames+".deleteBy"+methodNameStr+"("+paramsName+");");
                bw.newLine();
                bw.write("\t\treturn getSuccessResponseVO(null);");
                bw.newLine();
                bw.write("\t}");
                bw.newLine();
                bw.newLine();
            }

            bw.write("}");
            bw.newLine();


        } catch (Exception e) {
            logger.error("创建Mapper失败", e);
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (outw != null) {
                try {
                    outw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        }
    }

}

